package com.yuju.group.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.cache.RedisCache;
import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.constant.CacheKey;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.SeqCodeEnum;
import com.yuju.enums.StatusEnum;
import com.yuju.exception.BusinessException;
import com.yuju.group.dao.IHotelDao;
import com.yuju.group.dao.entity.Hotel;
import com.yuju.group.enums.GroupReturnCodeEnum;
import com.yuju.group.enums.HotelReturnCodeEnum;
import com.yuju.group.enums.SeqCodeReturnCodeEnum;
import com.yuju.group.model.CityModel;
import com.yuju.group.model.HotelModel;
import com.yuju.group.model.ProvinceModel;
import com.yuju.group.model.TownModel;
import com.yuju.group.service.ICodeGenService;
import com.yuju.group.service.IGroupService;
import com.yuju.group.service.IHotelService;
import com.yuju.group.service.ISeqCodeService;
import com.yuju.hotel.enums.CheckinStandardReturnCodeEnum;
import com.yuju.hotel.enums.PriceSchemeReturnCodeEnum;
import com.yuju.hotel.service.ICheckinStandardService;
import com.yuju.hotel.service.IHotelParamService;
import com.yuju.hotel.service.IPriceSchemeService;
import com.yuju.login.enums.OpReturnCodeEnum;
import com.yuju.login.service.IOpService;
import com.yuju.util.Utils;
/**
 * 
 * 类名：HotelServiceImpl  <br />
 *
 * 功能：
 *
 * @author : sunyl <br />
 * @Date : 2017年11月24日 上午9:37:41  <br />
 * @version : 1.1 <br />
 */
@Service
public class HotelServiceImpl extends AbstractBaseService<HotelModel,Hotel,String> implements IHotelService{
	@Autowired
	private IHotelDao hotelDao;
	@Autowired
	private ISeqCodeService seqCodeService;
	@Autowired
	private IPriceSchemeService priceSchemeService;
	@Autowired
	private ICheckinStandardService checkinStandardService;
	@Autowired
	private IOpService opService;
	@Autowired
	private ICodeGenService codeGenService;
	@Autowired
	private IGroupService groupService;
	
	@Autowired
	private IHotelParamService hotelParamService;
	
	@Override
	public IBaseDao<HotelModel, Hotel, String> getDao() {
		// TODO Auto-generated method stub
		return hotelDao;
	}

	/**
	 * 
	 * 功能：创建酒店时初始化班次表中酒店初始班次信息。
	 * 包括新增班次信息和初始化缓存班次信息。<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月21日 下午2:02:10 <br/>
	 */
	private int initShiftInfo(String groupId, String hotelId, String hotelCode) {
		String code = codeGenService.generate(groupId,hotelId,hotelCode,"CLASS_CODE");
		Map<String,Object> map = new HashedMap();
		map.put("groupId", groupId);
		map.put("hotelId", hotelId);
		map.put("code", code);
		String id = Utils.getUUID();
		map.put("id", id);

		RedisCache simpleCache = RedisCache.getInstance();
		String key = hotelId + CacheKey.KEY_CACHE_CLASSNO;
		simpleCache.addCache(key, map);

		return hotelDao.initShiftInfo(map);
	}
	@Override
	public int updateSelective(HotelModel model) {
		if(!validRepeatName(model.getId(), model.getName(), model.getGroupId())){
			throw new BusinessException(ReturnCodeEnum.VALID_REPEAT_FAILURE.getCode(),
					ReturnCodeEnum.VALID_REPEAT_FAILURE.getDesc());
		}
		model.setGmtModified(new Date());
		// 更新数据
		return super.updateSelective(model);
	}
	/**
	 * 
	 * 功能：<br/>
	 *更新酒店是集团的分店姓名
	 * @author sunyl
	 * @Date 2017年12月22日 下午4:26:33 <br/>
	 */
	@Override
	public int updateSelectiveForUnValidForGroupName(String groupId,String name) {
		HotelModel hotelModel = new HotelModel();
		hotelModel.setIsGroup(StatusEnum.TRUE.getValue());
		hotelModel.setGroupId(groupId);
		HotelModel hotelModelUpdate = super.find(hotelModel);
		if(hotelModelUpdate == null){
			throw new BusinessException(HotelReturnCodeEnum.Hotel_QUERY_GROUP_FAILURE.getCode(),
					HotelReturnCodeEnum.Hotel_QUERY_GROUP_FAILURE.getDesc());
		}
		hotelModelUpdate.setName(name);
		hotelModelUpdate.setGmtModified(new Date());
		// 更新数据
		return super.updateSelective(hotelModelUpdate);
	}
	@Override
	public int save(HotelModel model) {
		if(!validRepeatName(model.getId(), model.getName(), model.getGroupId())){
			throw new BusinessException(ReturnCodeEnum.VALID_REPEAT_FAILURE.getCode(),
					ReturnCodeEnum.VALID_REPEAT_FAILURE.getDesc());
		}
		/*生成集团是酒店编号在此生成*/
		/*if(model.getIsGroup()!=null && model.getIsGroup()== StatusEnum.TRUE.getValue()){*/
			String hotelCode  = codeGenService.generate(model.getGroupId(), null, null,SeqCodeEnum.HOTEL_CODE.getCode());
			if(hotelCode == null){
				throw new BusinessException(SeqCodeReturnCodeEnum.SEQ_HOTEL_SAVE_FAILURE.getCode(),
						SeqCodeReturnCodeEnum.SEQ_HOTEL_SAVE_FAILURE.getDesc());
			}
			model.setCode(hotelCode);
			/*}*/
		// 保存数据
		String id = Utils.getUUID();
		model.setId(id);
		model.setGmtCreate(new Date());
		model.setGmtModified(new Date());
		int rows = super.save(model);
		if(!seqCodeService.initHotelSeqCode(model.getGroupId(),id)){
			throw new BusinessException
			(SeqCodeReturnCodeEnum.SEQ_HOTEL_SAVE_FAILURE.getCode(),
					SeqCodeReturnCodeEnum.SEQ_HOTEL_SAVE_FAILURE.getDesc());
		}
		if(priceSchemeService.saveHotelAfter(id, model.getGroupId())==0){
			throw new BusinessException(
					PriceSchemeReturnCodeEnum.PRICESCHEME_SAVE_FAILURE.getCode(),
					PriceSchemeReturnCodeEnum.PRICESCHEME_SAVE_FAILURE.getDesc());
		}
		if(checkinStandardService.saveHotelAfter(id, model.getGroupId())==0){
			throw new BusinessException(
					CheckinStandardReturnCodeEnum.CHECKINSTANDARD_SAVE_FAILURE.getCode(),
					CheckinStandardReturnCodeEnum.CHECKINSTANDARD_SAVE_FAILURE.getDesc());
		}
		if(opService.saveHotelAfter(id, model.getGroupId())==0){
			throw new BusinessException(OpReturnCodeEnum.OP_SAVE_FAILURE.getCode(), 
					OpReturnCodeEnum.OP_SAVE_FAILURE.getDesc());
		}
		if(groupService.updateForHotelCount(model.getGroupId())==0){
			throw new BusinessException
			(GroupReturnCodeEnum.GROUP_UPDTAE_HOTELCOUTS_FAILURE.getCode(),
					GroupReturnCodeEnum.GROUP_UPDTAE_HOTELCOUTS_FAILURE.getDesc());
		}
		//初始化酒店的班次信息
		initShiftInfo(model.getGroupId(), id, model.getCode());
		
		// 初始化酒店参数
		hotelParamService.initHotelParam(model.getGroupId(), id);
		return rows;
	}
	/**
	 * 获取酒店编号
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月9日 上午11:47:16 <br/>
	 */
	@Override
	public String getHotelCode(String groupId) {
		String hotelCode = codeGenService.generate(groupId, null,null,SeqCodeEnum.HOTEL_CODE.getCode());
		if(hotelCode==null){
			throw new BusinessException
			(ReturnCodeEnum.ORDER_NO_GEN_FALURE.getCode(), ReturnCodeEnum.ORDER_NO_GEN_FALURE.getDesc());
		}
		return hotelCode;
	}
	@Override
	public int del(String id) {
		int i = 0;
		HotelModel hotelModel = super.findById(id);
		if(hotelModel != null){
			i = super.del(id);
			if(groupService.updateForHotelCount(hotelModel.getGroupId())==0){
				throw new BusinessException
				(GroupReturnCodeEnum.GROUP_UPDTAE_HOTELCOUTS_FAILURE.getCode(),
						GroupReturnCodeEnum.GROUP_UPDTAE_HOTELCOUTS_FAILURE.getDesc());
			}
		}
		return i;
	}
	
	/**
	 * 恢复营业
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月18日 下午6:21:38 <br/>
	 */
	@Override
	public int updateStatusForRecover(String id) {
		int i = 0;
		HotelModel hotelModel = super.findById(id);
		if(hotelModel != null){
			hotelModel.setGmtModified(new Date());
			hotelModel.setIsEnable(StatusEnum.TRUE.getValue());
			i = super.updateSelective(hotelModel);
			if(groupService.updateForHotelCount(hotelModel.getGroupId())==0){
				throw new BusinessException
				(GroupReturnCodeEnum.GROUP_UPDTAE_HOTELCOUTS_FAILURE.getCode(),
						GroupReturnCodeEnum.GROUP_UPDTAE_HOTELCOUTS_FAILURE.getDesc());
			}
		}
		return i;
	}
	/**
	 * 停业
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月18日 下午6:21:45 <br/>
	 */
	@Override
	public int updateStatusForStop(String id) {
		int i = 0;
		HotelModel hotelModel = super.findById(id);
		if(hotelModel != null){
			hotelModel.setGmtModified(new Date());
			hotelModel.setIsEnable(StatusEnum.FALSE.getValue());
			i = super.updateSelective(hotelModel);
			if(groupService.updateForHotelCount(hotelModel.getGroupId())==0){
				throw new BusinessException
				(GroupReturnCodeEnum.GROUP_UPDTAE_HOTELCOUTS_FAILURE.getCode(),
						GroupReturnCodeEnum.GROUP_UPDTAE_HOTELCOUTS_FAILURE.getDesc());
			}
		}
		return i;
	}
	/**
     * 重命名验证
     * 功能：<br/>
     *
     * @author sunyl
     * @Date 2017年12月21日 下午8:00:32 <br/>
     */
	@Override
	public boolean validRepeatName(String id, String name,String groupId){
		if(id != null){
			HotelModel hotelModel = super.findById(id);
			if(hotelModel == null){
				throw new BusinessException(HotelReturnCodeEnum.Hotel_QUERY_FAILURE.getCode(),
						HotelReturnCodeEnum.Hotel_QUERY_FAILURE.getDesc());
			}
			if(hotelModel.getName().equals(name)){
				return true;
			}else{
				return validRepeatNameForNullId(name, groupId);
			}
		}else{
			return validRepeatNameForNullId(name, groupId);
		}
	};
	private boolean validRepeatNameForNullId(String name,String groupId){
		HotelModel hotelModelValid = new HotelModel();
		hotelModelValid.setGroupId(groupId);
		hotelModelValid.setName(name);
		HotelModel hotelModelValidResult = super.find(hotelModelValid);
		if(hotelModelValidResult == null){
			return true ;
		}else{
			return false;
		}
	}
	@Override
	public List<ProvinceModel> selectProvinceList(Map<Object, Object> map) {
		return hotelDao.selectProvinceList(map);
	}

	@Override
	public List<CityModel> selectCityList(Map<Object, Object> map) {
		return hotelDao.selectCityList(map);
	}

	@Override
	public List<TownModel> selectTownList(Map<Object, Object> map) {
		return hotelDao.selectTownList(map);
	}

	@Override
	public List<CityModel> selectHotelCity(Map<Object, Object> map) {
		return hotelDao.selectHotelCity(map);
	}

	@Override
	public List<TownModel> selectHotelTown(Map<Object, Object> map) {
		return hotelDao.selectHotelTown(map);
	}
}
